package com.ruoyi.writer.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.domain.ChapterDTO;
import com.ruoyi.domain.NewNovel;
import com.ruoyi.domain.NovelDTO;
import com.ruoyi.domain.NovelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.writer.mapper.ChapterMapper;
import com.ruoyi.writer.domain.Chapter;
import com.ruoyi.writer.service.IChapterService;

/**
 * 写作Service业务层处理
 * 
 * @author fst
 * @date 2025-04-10
 */
@Service
public class ChapterServiceImpl implements IChapterService 
{
    @Autowired
    private ChapterMapper chapterMapper;

    /**
     * 查询写作
     * 
     * @param id 写作主键
     * @return 写作
     */
    @Override
    public Chapter selectChapterById(Long id)
    {
        return chapterMapper.selectChapterById(id);
    }

    /**
     * 查询写作列表
     * 
     * @param chapter 写作
     * @return 写作
     */
    @Override
    public List<Chapter> selectChapterList(Chapter chapter)
    {
        return chapterMapper.selectChapterList(chapter);
    }

    /**
     * 新增写作
     * 
     * @param chapter 写作
     * @return 结果
     */
    @Override
    public int insertChapter(Chapter chapter)
    {
        chapter.setCreateTime(DateUtils.getNowDate());
        return chapterMapper.insertChapter(chapter);
    }

    /**
     * 修改写作
     * 
     * @param chapter 写作
     * @return 结果
     */
    @Override
    public int updateChapter(Chapter chapter)
    {
        chapter.setUpdateTime(DateUtils.getNowDate());
        return chapterMapper.updateChapter(chapter);
    }

    /**
     * 批量删除写作
     * 
     * @param ids 需要删除的写作主键
     * @return 结果
     */
    @Override
    public int deleteChapterByIds(Long[] ids)
    {
        return chapterMapper.deleteChapterByIds(ids);
    }

    /**
     * 删除写作信息
     * 
     * @param id 写作主键
     * @return 结果
     */
    @Override
    public int deleteChapterById(Long id)
    {
        return chapterMapper.deleteChapterById(id);
    }

    @Override
    public List<NovelDTO> getUserNovel(Long userId) {
        List<NovelDTO> novelDTOS = chapterMapper.getUserNovel(userId);
        return novelDTOS;
    }

    @Override
    public AjaxResult updateNovel(NovelVo novelVo) {
        return AjaxResult .success(chapterMapper.updateNovel(novelVo));
    }

    @Override
    public int InsertNovel(ChapterDTO chapterDTO) {
        Long novelId = chapterMapper.selectNovelId(chapterDTO.getTitle());
        chapterDTO.setNovelId(novelId);
        // 将该章节的字数累加到小说总字数中、
        Long wordCount = chapterDTO.getWordCount();
        Long count = chapterMapper.selectWordCount(novelId);
        long lastWordCount = wordCount + count;
        chapterMapper.updateNovelWordCount(lastWordCount, novelId);
        return chapterMapper.InsertChapter(chapterDTO);
    }

    @Override
    public int updateChapterContent(ChapterDTO chapterDTO) {
        Long novelId = chapterMapper.selectNovelId(chapterDTO.getTitle());
        chapterDTO.setNovelId(novelId);
        return chapterMapper.updateChapterContent(chapterDTO);
    }

    @Override
    public int addNovel(NewNovel newNovel) {
        String authorName = chapterMapper.selectAuthorNameByUserId(newNovel.getAuthorId());
        Long categoryId = chapterMapper.selectcategoryId(newNovel.getCategoryName());
        newNovel.setAuthorName(authorName);
        newNovel.setCategoryId(categoryId);
        newNovel.setStatus("1");
        return  chapterMapper.insertNovel(newNovel);
    }
}
